package com.skyc.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.skyc.entity.Order;
import com.skyc.entity.User;
import com.skyc.service.OrderService;
import com.skyc.service.impl.OrderServiceImpl;

/**
 * Servlet implementation class AServlet
 */
@WebServlet("/ToPayServlet")
public class ToPayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	protected void doGet(HttpServletRequest req, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(req, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		HttpSession session=request.getSession();
		ServletContext application=getServletContext();		
		PrintWriter out=response.getWriter();
		OrderService os = new OrderServiceImpl();
		User user=(User) session.getAttribute("user");
		String uid=user.getUid()+"";	
		List<Integer> oid=(List<Integer>) session.getAttribute("oid");
		for(Integer c:oid) {
			os.updateOpayedByOid(c+"");
		}
        int flag=(int) session.getAttribute("flag");
        if(flag==1) {
        	int countPayed = (int) session.getAttribute("countPayed")-oid.size();
    		int countSend = (int) session.getAttribute("countSend")+oid.size();
    		session.setAttribute("countPayed", countPayed);
    		session.setAttribute("countSend", countSend);
    		request.getRequestDispatcher("myorder.jsp").forward(request, response);
        }else {
        	int countState = 0;
			int countPayed = 0;
			int countSend = 0;
			int pingjia = 0;
			int countall = 0;
			List<Order> orderlist = os.queryByUid(uid);
			for (Order c : orderlist) {
				c.getClothes()
						.setCimgurl(c.getClothes().getCimgurl().substring(0, c.getClothes().getCimgurl().indexOf('/')));
				if (c.getOstate() == 0 && c.getOpayed() == 1 && c.getOsend() == 1) {
					countState++;// 待收货
				}
				if (c.getOpayed() == 0 && c.getOstate() == 0 && c.getOsend() == 0) {
					countPayed++;// 待付款
				}
				if (c.getOsend() == 0 && c.getOpayed() == 1 && c.getOstate() == 0) {
					countSend++;// 待发货
				}
				if (c.getOsend() == 1 && c.getOpayed() == 1 && c.getOstate() == 1&&c.getOtuihuo()!=3) {
					pingjia++;// 待评价
				}
				if(c.getOtuihuo()!=3) {
					countall++;
					}
			}
			session.setAttribute("countall", countall);
			session.setAttribute("countState", countState);
			session.setAttribute("countPayed", countPayed);
			session.setAttribute("countSend", countSend);
			session.setAttribute("countpingjia", pingjia);
			session.setAttribute("alllist", orderlist);
			session.setAttribute("orderlist", orderlist);
			request.getRequestDispatcher("myorder.jsp").forward(request, response);
        }				
		out.flush();
		out.close();
	}

}
